package com.lyl.serviceacl.controller;

import com.alibaba.fastjson.JSONObject;
import com.lyl.commonutils.JwtUtils;
import com.lyl.commonutils.R;
import com.lyl.serviceacl.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
////import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/serviceacl/admin/index")
public class IndexController {

    @Autowired
    private IndexService indexService;

    /**
     * 1.登录: serviceacl/admin/index/login  --> token
     * 2.获取用户信息: 安全验证框架 拦截--> 通过你的requset 去获取token 根据token 信息
     *
     * 前台登录: token id, 用户名
     * 后台登录: token: 用户名
     */


    /**
     * 根据token获取用户信息
     *
     */
    @PostMapping(value = "info")
   public R info(){
        System.out.println("获取用户信息");
        //获取当前登录用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//        String username="admin";
        Map<String, Object> userInfo = indexService.getUserInfo(username);

        return R.ok().data(userInfo);
    }

    /**
     * 获取菜单
     * @return
     */
    @GetMapping("menu")
    public R getMenu(HttpServletRequest request){
        //获取当前登录用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//        String username="admin";
        List<JSONObject> permissionList = indexService.getMenu(username);
        return R.ok().data("permissionList", permissionList);
    }

    @PostMapping("logout")
    public R logout(){
        System.out.println("退出");

        return R.ok();
    }

}
